package com.hp.bon.sgw.ctrl;

import org.productivity.java.syslog4j.server.SyslogServer;
import org.productivity.java.syslog4j.server.SyslogServerConfigIF;
import org.productivity.java.syslog4j.server.SyslogServerIF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hp.bon.sgw.service.SyslogFilterEventHandler;

public class SysLogServerStarter {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysLogServerStarter.class);

	private String protocol = "udp";
	private String host = "0.0.0.0";
	private int port = 8899;
	private SyslogServerIF servrIF;

	private SyslogFilterEventHandler syslogFilterEventHandler;

	public void start() throws Exception {
		// UDPNetSyslogServerConfig syslogConf = new UDPNetSyslogServerConfig();

		logger.info("Start Syslog Server at " + host + ":" + port);
		servrIF = SyslogServer.getInstance(protocol);
		SyslogServerConfigIF syslogConf = servrIF.getConfig();
		syslogConf.setHost(this.host);
		syslogConf.setPort(this.port);
		syslogConf.addEventHandler(syslogFilterEventHandler);
		servrIF = SyslogServer.getThreadedInstance(protocol);
	}

	public void shutdown() {
		logger.info("shutdown syslog server");
		servrIF.shutdown();
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@Autowired
	public void setSyslogFilterEventHandler(SyslogFilterEventHandler syslogFilterEventHandler) {
		this.syslogFilterEventHandler = syslogFilterEventHandler;
	}

}
